package com.gitee.easyopen.exception;

import com.gitee.easyopen.message.Error;
import com.gitee.easyopen.message.Errors;

public class ApiException extends RuntimeException {
    private static final long serialVersionUID = 16789476595630713L;

    private Error<String> error;
    private Object data;

    public ApiException(String msg) {
        super(msg);
        this.error = new SysError(msg);
    }

    public ApiException(Exception e) {
        this(e.getMessage());
    }

    public ApiException(Error<String> error) {
        super(error.getMsg());
        this.error = error;
    }

    public ApiException(Error<String> error, Object data) {
        this(error);
        this.data = data;
    }

    public Error<String> getError() {
        return error;
    }

    public void setError(Error<String> error) {
        this.error = error;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    private static class SysError implements Error<String> {

        private String msg;

        public SysError(String msg) {
            super();
            this.msg = msg;
        }

        @Override
        public String getMsg() {
            return msg;
        }

        @Override
        public String getCode() {
            return Errors.SYS_ERROR.getCode();
        }
    }

}
